package com.spc.car.web.controller.base;

import com.spc.car.core.framework.web.domain.AjaxResult;
import com.spc.car.data.domain.base.po.ProjectTypeQueryPO;
import com.spc.car.data.domain.base.vo.ProjectTypeTreeVO;
import com.spc.car.data.entity.base.BaseProjectType;
import com.spc.car.data.service.base.BaseProjectTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

@Validated
@Api(tags = "项目类型")
@RequestMapping("/base/projectType")
@RestController
public class ProjectTypeController {

    @Autowired
    private BaseProjectTypeService projectTypeService;

    @ApiOperation(value = "获取项目类型", notes = "获取项目类型")
    @PostMapping("/listProjectType")
    public AjaxResult listProjectType(@Valid @RequestBody(required = false) ProjectTypeQueryPO projectTypeQueryPO) {

        List<BaseProjectType> result = null == projectTypeQueryPO ?
                projectTypeService.list() : projectTypeService.list(projectTypeQueryPO.toWrapper());

        return AjaxResult.success("获取项目类型成功", result);
    }

    @ApiOperation(value = "获取项目类型树", notes = "获取项目类型树")
    @PostMapping("/listProjectTypeTree")
    public AjaxResult listProjectTypeTree(@Valid @RequestBody(required = false) ProjectTypeQueryPO projectTypeQueryPO) {

        List<BaseProjectType> result = null == projectTypeQueryPO ?
                projectTypeService.list() : projectTypeService.list(projectTypeQueryPO.toWrapper());

        List<ProjectTypeTreeVO> treeList = toTree(result);
        return AjaxResult.success("获取项目类型树成功", treeList);
    }

    private List<ProjectTypeTreeVO> toTree(List<BaseProjectType> sourceList) {
        List<ProjectTypeTreeVO> resultList = new ArrayList<>();

        if (!CollectionUtils.isEmpty(sourceList)) {
            Integer minLevel = sourceList.stream().map(BaseProjectType::getLevel)
                    .min(Integer::compareTo).get();

            List<ProjectTypeTreeVO> tmpTreeList = toTreeNodes(sourceList);
            tmpTreeList.stream().filter(tmp -> minLevel.equals(tmp.getLevel())).forEach(project -> {
                resultList.add(recurse(tmpTreeList, project));
            });
        }
        return resultList;
    }


    public ProjectTypeTreeVO recurse(List<ProjectTypeTreeVO> source, ProjectTypeTreeVO currentNode) {
        List<ProjectTypeTreeVO> children = source.stream()
                .filter(projectType -> currentNode.getCode().equals(projectType.getParentCode()))
                .collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(children)) {
            currentNode.setTreeNode(new ArrayList<>());
            children.forEach(child -> {
                currentNode.getTreeNode().add(recurse(source, child));
            });
        }

        return currentNode;
    }

    public List<ProjectTypeTreeVO> toTreeNodes(List<BaseProjectType> sourceList) {
        List<ProjectTypeTreeVO> targetList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(sourceList)) {
            sourceList.forEach(source -> {
                ProjectTypeTreeVO projectTypeTreeVO = new ProjectTypeTreeVO();
                BeanUtils.copyProperties(source, projectTypeTreeVO);
                targetList.add(projectTypeTreeVO);
            });
        }
        return targetList;
    }

}
